package com.ttbj.order.component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ttbj.constant.ProjectConfig;
import com.ttbj.core.dao.redis.IRedisValueBaseDao;
import com.ttbj.product.bean.dto.OrderDto;

@Component
public class OrderCacheComponent {

	@Autowired
	private IRedisValueBaseDao<LinkedBlockingQueue<Long>> redisValueDao = null;

	/**
	 * 获取
	 * 
	 * @param key
	 * @return
	 */
	private LinkedBlockingQueue<Long> getList() {

		return redisValueDao.get(ProjectConfig.KEY);
	}

	/**
	 * 取出订单编号
	 * 
	 * @return
	 * @throws Exception
	 */
	public Long get() throws Exception {
		LinkedBlockingQueue<Long> queue = getList();
		if (null == queue || queue.isEmpty()) {
			return null;
		}
		System.out.println("-----+"+queue.size());
		Long orderId = queue.poll();
		redisValueDao.set(ProjectConfig.KEY, queue);
		return orderId;
	}

	/**
	 * 设置订单编号
	 * 
	 * @param dto
	 * @return
	 * @throws InterruptedException
	 */
	public boolean set(Long orderId) throws InterruptedException {
		LinkedBlockingQueue<Long> queue = getList();
		if (null == queue) {
			queue = new LinkedBlockingQueue<Long>();
		}
		if(!queue.contains(orderId)){
			queue.offer(orderId);
		}
		redisValueDao.set(ProjectConfig.KEY, queue);
		return true;
	}
	
	 

}
